package de.tum.in.eist.poll.client;

import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

import de.tum.in.eist.poll.pub.shared.Lecture;

/**
 * This class builds the Statistic-View of the Admins
 */
public class AdminStatisticView extends VerticalPanel {
	private Chart chart;
	private Button back = new Button();
	private TabPanel tabPanel = new TabPanel();
	
	
	/**
	 * Initializes an AdminStatisticView-object
	 * 
	 * @param lecture is the lecture of the statistics
	 */
	public AdminStatisticView(Lecture lecture){
		chart = new Chart(lecture, 350, 200, 1, this);
		chart.addStyleName("userChart");
		this.add(chart);
		Button update = new Button();
		update.setText("update");
		update.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				chart.update();
				
			}
		});
		
		back.setText("Back");
		back.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				RootPanel root = RootPanel.get("main");
				root.clear();
				root.add(new AdminSelectLectureView());
				RootPanel.get("back").clear();
			}
		});
		//this.add(update);
		tabPanel.setVisible(false);
		this.add(tabPanel);
		
		RootPanel.get("back").add(back);
		//chart.update();
	}

	/**
	 * Adds to the tabPanel a likes and a dislikes-Panel
	 */
	public void makeLikes() {
		Stats data = chart.getData();
		if (data.getOpinions() == null){
			return;
		}
		List<String> likes = data.getLike();
		List<String> disLikes = data.getDislike();
		VerticalPanel likePanel = new VerticalPanel();
		VerticalPanel dislikePanel = new VerticalPanel();
		if(likes != null){
			ListIterator<String> iterator = data.getLike().listIterator();
			while(true){
				try{
					String text = iterator.next();
					if(!text.equals(""))
							likePanel.add(new Label(text));
				}
				catch(NoSuchElementException e){
					break;
				}
			}
		}
		if(disLikes != null){
			ListIterator<String> iterator = data.getDislike().listIterator();
			while(true){
				try{
					String text = iterator.next();
					if(!text.equals(""))
							dislikePanel.add(new Label(text));
				}
				catch(NoSuchElementException e){
					break;
				}
			}
		}
		tabPanel.add(likePanel, "Likes");
		tabPanel.add(dislikePanel, "Dislikes");
		tabPanel.selectTab(0);
		tabPanel.setVisible(true);
	}
}
